//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using System.Text;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for TagSelector.
	/// </summary>
	public class TagSelector : TemplatedWebControl
	{
		protected PlaceHolder CheckboxListContainer;
		protected IButton OK;

		#region Properties

		int _checkboxListColumns = 2;
		public int CheckboxListColumns
		{
			get
			{
				return _checkboxListColumns;
			}
			set
			{
				_checkboxListColumns = value;
			}
		}

		#endregion

		#region Skin

		protected override void AttachChildControls() 
		{
			CheckboxListContainer = (PlaceHolder) FindControl("CheckboxListContainer");
			OK = FindButton("OK");

			OK.Attributes.Add("onclick", string.Format("{0}.SaveTags(); return false;", this.ClientID));
		}

		#endregion

		#region Control Event Handlers

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			
			DataBind();
		}

		public override void DataBind()
		{
			base.DataBind ();

			Head.AddTitle(ResourceManager.GetString("TagSelector_Title"), this.Context);

			CheckboxListContainer.Controls.Add(new LiteralControl(string.Format("<div id=\"{0}_CheckBoxList\"></div>", this.ClientID)));
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (!this.Page.IsClientScriptBlockRegistered(this.GetType().FullName))
				this.Page.RegisterClientScriptBlock(this.GetType().FullName, string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/TagSelector.js")));

			if (!this.Page.IsStartupScriptRegistered(this.ClientID))
				this.Page.RegisterStartupScript(this.ClientID, string.Format("<script language=\"javascript\">var {0} = new TagSelector('{0}', '{0}_CheckBoxList', '{1}', {2});</script>", this.ClientID, Page.Request.QueryString["TagEditor"].ToString(), this.CheckboxListColumns.ToString()));
		}

		#endregion
	}
}
